package jmind.core.test;

import com.google.common.collect.Maps;
import jmind.base.util.IOUtil;
import jmind.core.http.HttpClient4;
import jmind.core.image.ImageUtil;
import jmind.core.support.HttpUtil;
import jnr.ffi.annotations.In;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.StringJoiner;

/**
 * description:
 *
 * @author weibo.xie
 * @date : create in 3:37 下午 2021/8/6
 */
public class ZGCX {

    static String url = "http://zgcx.nhc.gov.cn:9090/unit/index";
    static String ua="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36";
    static String imageUrl="http://zgcx.nhc.gov.cn:9090/CaptchaGenerate/Generate";
    public static void main(String[] args) throws Exception {

        String s="123456";
        int number=0;
        for(int i=0;i<s.length();i++){
            char ch=s.charAt(i);

            System.err.println(ch-'0');
        }

        System.err.println(number);





    }

    public static void doCheckCode() throws Exception {
        Map<String,String> cookies1= Maps.newHashMap();
        cookies1.put("__RequestVerificationToken","M6DxZsGa_mIU07Pnt9Qjy5HKmISg22Y-yXmFgHPPqmQ6G7ER2qgsywRuL-B_4PRFCLdWoZtEKIxFRo7rIsBuRkHwsu0qwq4KWR5T5X-THn01");
        cookies1.put("Doct_SessionId","jfztgg1i4tutqzmnj412iev2");
        Connection.Response response = Jsoup.connect(url).userAgent(ua).cookies(cookies1).execute();
        System.err.println("headers=" + response.headers());
        Map<String, String> cookies = response.cookies();
        System.err.println("cookies=" + cookies);

        String body = response.body();
        Document document = Jsoup.parse(body);
        Elements option = document.select("#Prov").select("option");
//        option.forEach(element -> {
//            String vv= element.attr("value")+"="+element.text();
//            System.err.println(vv);
//
//        });
      //  saveImage(imageUrl,"/Users/weibo/Downloads/a/yan.png",toCookieString(cookies));
        Map<String,String> params= Maps.newHashMap();
        params.put("Prov","11");
        params.put("Unit_Name","朝阳医院");
        params.put("Check_Code","8rzipx".toUpperCase());
        params.put("__RequestVerificationToken",cookies1.get("__RequestVerificationToken"));
        query(params,response.headers(),cookies1);
    }

    public static void doQuery(){
        Map<String,String> cookies= Maps.newHashMap();
        cookies.put("__RequestVerificationToken","M6DxZsGa_mIU07Pnt9Qjy5HKmISg22Y-yXmFgHPPqmQ6G7ER2qgsywRuL-B_4PRFCLdWoZtEKIxFRo7rIsBuRkHwsu0qwq4KWR5T5X-THn01");
        cookies.put("Doct_SessionId","jfztgg1i4tutqzmnj412iev2");
        Map<String,String> params= Maps.newHashMap();
        params.put("Prov","11");
        params.put("Unit_Name","朝阳医院");
        params.put("Check_Code","8rzipx".toUpperCase());
        params.put("__RequestVerificationToken",cookies.get("__RequestVerificationToken"));
        query(params,null,cookies);
    }

    public  static void query(Map<String,String> params,Map<String,String> headers,Map<String,String> cookies){
        try {
            Document doc = Jsoup.connect(url).data(params).userAgent(ua).headers(headers).cookies(cookies).method(Connection.Method.POST).execute().parse();
            System.err.println(doc.html());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


     public static String toCookieString(Map<String,String> cookies){
         StringJoiner sb=new StringJoiner(";");
        cookies.forEach((k,v)->{
            sb.add(k+"="+v);
        });
        String s= sb.toString();
         System.err.println(s);
        return s;
     }
    public static File saveImage(String imageUrl,String destFile,String cookie) throws Exception {
        File f = new File(destFile);
        File parent = f.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        URL url = new URL(imageUrl);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Cookie", cookie);
        InputStream is = url.openStream();
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buff = new byte[1024 * 8];
        int read = 0;
        while ((read = is.read(buff)) > 0) {
            fos.write(buff, 0, read);
        }
        fos.close();
        is.close();
        return f;
    }
}